var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var face = require('./routes/face');
var imageClassify = require('./routes/imageClassify');
var contentCensor = require('./routes/contentCensor');
var nlp = require('./routes/nlp');
var speech = require('./routes/speech');

var app = express();

app.all('*', function (req, res, next) {
  if (req.headers.origin) {
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Credentials", true);
  }
  next();
});

// uncomment after placing your favicon in /public
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


app.use('/bd/', index);
app.use('/bd/face/', face);
app.use('/bd/imageClassify/', imageClassify);
app.use('/bd/contentCensor/', contentCensor);
app.use('/bd/nlp/', nlp);
app.use('/bd/speech/', speech);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.send({ err: 'error' });
});

module.exports = app;
